package com.yt.lxh.product.controller;


import com.yt.lxh.common.result.ResponseResult;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.vo.product.SimpleSkuVO;
import com.yt.lxh.vo.product.SkuVO;
import com.yt.lxh.dto.product.SaveSkuDTO;
import com.yt.lxh.dto.product.UpdateSkuDTO;
import com.yt.lxh.product.service.SkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品口味管理接口
 *
 * @author yzk789
 * @since 2023-10-18
 */
@Api(tags = "商品管理接口")
@RestController
@RequestMapping("/admin/product/sku")
//@CrossOrigin
public class SkuController {

    @Autowired
    private SkuService skuService;

    @ApiOperation("添加商品")
    @PostMapping("/save")
    public ResponseResult saveSku(@Validated SaveSkuDTO saveSkuDTO) {
            skuService.saveSku(saveSkuDTO);
        return ResponseResult.ok(ResultCodeEnum.INSERT_SUCCESS);
    }

    @ApiOperation("根据ID获取商品数据")
    @GetMapping("/{id}")
    public ResponseResult getSkuById(@PathVariable String id) {
        SkuVO skuVO = skuService.getSkuById(id);
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, skuVO);
    }

    @ApiOperation("根据类别ID获取商品数据")
    @GetMapping("/by-category/{categoryId}")
    public ResponseResult getSkuByCategoryId(@PathVariable String categoryId) {
        List<SimpleSkuVO> simpleSkuVOS = skuService.simpleSkuListByCategoryId(categoryId);
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, simpleSkuVOS);
    }

    @ApiOperation("更新商品数据")
    @PutMapping("/update")
    public ResponseResult updateSku(@Validated UpdateSkuDTO updateSkuDTO) {
        skuService.updateSku(updateSkuDTO);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("根据类别ID修改商品售卖状态")
    @PutMapping("/{id}/{onSale}")
    public ResponseResult updateSkuOnSaleById(@PathVariable String id, @PathVariable boolean onSale) {
        skuService.updateSkuOnSaleById(id, onSale);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("根据类别ID逻辑删除数据")
    @DeleteMapping("/{id}")
    public ResponseResult delSkuById(@PathVariable String id) {
        skuService.removeSkuById(id);
        return ResponseResult.ok(ResultCodeEnum.DELETE_SUCCESS);
    }
}

